package com.maxlen.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.admin.entity.Permission;
import com.maxlen.admin.entity.Role;
import com.maxlen.admin.entity.RolePermission;
import com.maxlen.admin.entity.dto.PageParamDto;
import com.maxlen.admin.entity.vo.PermissionVo;
import com.maxlen.admin.entity.vo.RolePermissionVo;
import com.maxlen.admin.mapper.RoleMapper;
import com.maxlen.admin.service.IAdminRoleService;
import com.maxlen.admin.service.IPermissionService;
import com.maxlen.admin.service.IRolePermissionService;
import com.maxlen.admin.service.IRoleService;
import com.maxlen.admin.utils.WrapperUtils;
import com.maxlen.common.utils.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-10-11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Override
    @Transactional
    public boolean add(RolePermissionVo rolePermissionVo) {
        Role role = new Role(rolePermissionVo.getName());
        int insert = baseMapper.insert(role);
        if(insert > 0){
            rolePermissionService.addByPermissionIdList(role.getId(), rolePermissionVo.getPermissionId());
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean deleteById(Long id) {
        int deleteById = baseMapper.deleteById(id);
        if(deleteById > 0){
            adminRoleService.removeByRoleId(id);
            rolePermissionService.removeByRoleId(id);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateById(RolePermissionVo rolePermissionVo) {
        Long roleId = rolePermissionVo.getId();
        Role role = new Role(roleId, rolePermissionVo.getName());
        baseMapper.updateById(role);

        List<Long> permissionId = rolePermissionVo.getPermissionId();
        List<RolePermission> list = rolePermissionService.list(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, roleId));

        if(permissionId.isEmpty()){
            rolePermissionService.removeByRoleId(roleId);
            return true;
        }

        if(list.isEmpty()){
            rolePermissionService.addByPermissionIdList(roleId, permissionId);
            return true;
        }

        for (Long num : permissionId) {
            boolean a = false;
            for (RolePermission rolePermission : list) {
                if(num.equals(rolePermission.getPermissionId())){
                    a = true;
                    rolePermission.setPermissionId(0L);
                }
            }
            if(a == false){
                rolePermissionService.save(new RolePermission(roleId, num));
            }
        }
        for (RolePermission rolePermission : list) {
            Long aLong = rolePermission.getPermissionId();
            if(aLong != 0){
                rolePermissionService.removeByPermissionIdRoleId(roleId, aLong);
            }
        }

        return true;
    }

    @Override
    public List<String> findRoleNameByPermissionId(Long id) {
        List<String> roleNameList = baseMapper.findRoleNameByPermissionId(id);
        return roleNameList;
    }

    @Override
    public List<String> findRoleNameByAdminId(Long id) {
        List<String> roleNameList = baseMapper.findRoleNameByAdminId(id);
        return roleNameList;
    }

    @Override
    public List<PermissionVo> findPermissionByRoleId(Long id) {
        List<Permission> permissionList;
        if(id == -1){
            permissionList = permissionService.list();
        }else{
            permissionList = permissionService.findListByRoleId(id);
        }
        return CopyUtils.copyList(permissionList, PermissionVo.class);
    }

    @Override
    public Page<Role> findAllByPage(PageParamDto pageParamDto) {
        Page<Role> page = new Page<>(pageParamDto.getCurrentPage(), pageParamDto.getPageSize());
        return baseMapper.selectPage(page, WrapperUtils.getWrapper(pageParamDto, Role.class));
    }

    @Override
    @Transactional
    public void deleteByIdList(List<Long> multipleId) {
        for (Long num : multipleId) {
            deleteById(num);
        }
    }

    @Override
    public List<Role> findListByAdminId(Long id) {
        return baseMapper.findListByAdminId(id);
    }

}
